{% extends "admin/base_site.html" %}
{% load static %}

{% block title %}{{ user.username }} - 用户详情 - {{ site_title|default:"Django 管理" }}{% endblock %}

{% block extrahead %}
<style>
.user-dashboard {
    padding: 20px;
    background: #f8f9fa;
    min-height: calc(100vh - 120px);
}

.user-header {
    background: linear-gradient(135deg, #6f42c1 0%, #5a32a3 100%);
    color: white;
    padding: 30px;
    border-radius: 12px;
    margin-bottom: 30px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

.user-header h1 {
    margin: 0;
    font-size: 2.2em;
    font-weight: 300;
}

.user-header .user-info {
    margin-top: 15px;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
}

.user-info-item {
    background: rgba(255,255,255,0.1);
    padding: 15px;
    border-radius: 8px;
}

.user-info-item h3 {
    margin: 0 0 5px 0;
    font-size: 0.9em;
    opacity: 0.8;
}

.user-info-item .value {
    font-size: 1.5em;
    font-weight: bold;
}

.stats-overview {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.stat-card {
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    border-left: 4px solid #6f42c1;
}

.stat-card h3 {
    margin: 0 0 10px 0;
    color: #495057;
    font-size: 0.9em;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-card .value {
    font-size: 2em;
    font-weight: bold;
    color: #6f42c1;
    margin: 0;
}

.content-sections {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    margin-bottom: 30px;
}

.section {
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.section h2 {
    margin: 0 0 20px 0;
    color: #495057;
    font-size: 1.3em;
    border-bottom: 2px solid #e9ecef;
    padding-bottom: 10px;
}

.battle-item {
    padding: 15px;
    border-bottom: 1px solid #e9ecef;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.battle-item:last-child {
    border-bottom: none;
}

.battle-info {
    flex: 1;
}

.battle-opponent {
    font-weight: 500;
    color: #495057;
}

.battle-details {
    font-size: 0.85em;
    color: #6c757d;
    margin-top: 5px;
}

.battle-result {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.8em;
    font-weight: bold;
    text-align: center;
    min-width: 60px;
}

.result-win {
    background: #d4edda;
    color: #155724;
}

.result-loss {
    background: #f8d7da;
    color: #721c24;
}

.result-draw {
    background: #fff3cd;
    color: #856404;
}

.code-item {
    padding: 15px;
    border: 1px solid #e9ecef;
    border-radius: 8px;
    margin-bottom: 15px;
    transition: box-shadow 0.2s;
}

.code-item:hover {
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.code-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.code-title {
    font-weight: 500;
    color: #495057;
}

.code-language {
    background: #007cba;
    color: white;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.8em;
}

.code-status {
    font-size: 0.85em;
    margin-top: 5px;
}

.status-active {
    color: #28a745;
    font-weight: 500;
}

.status-inactive {
    color: #6c757d;
}

.opponent-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid #e9ecef;
}

.opponent-item:last-child {
    border-bottom: none;
}

.opponent-name {
    font-weight: 500;
    color: #495057;
}

.opponent-record {
    font-size: 0.9em;
    color: #6c757d;
}

.win-rate-bar {
    width: 100px;
    height: 6px;
    background: #e9ecef;
    border-radius: 3px;
    overflow: hidden;
    margin-top: 5px;
}

.win-rate-fill {
    height: 100%;
    background: linear-gradient(90deg, #dc3545, #ffc107, #28a745);
    transition: width 0.3s ease;
}

.full-width-section {
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    margin-bottom: 30px;
}
</style>
{% endblock %}

{% block content %}
<div class="user-dashboard">
    <!-- 用户头部信息 -->
    <div class="user-header">
        <h1>👤 {{ user.username }}</h1>
        <div class="user-info">
            <div class="user-info-item">
                <h3>总对战数</h3>
                <div class="value">{{ user_stats.total_battles }}</div>
            </div>
            <div class="user-info-item">
                <h3>胜率</h3>
                <div class="value">{{ user_stats.win_rate|floatformat:1 }}%</div>
            </div>
            <div class="user-info-item">
                <h3>胜场</h3>
                <div class="value">{{ user_stats.wins }}</div>
            </div>
            <div class="user-info-item">
                <h3>败场</h3>
                <div class="value">{{ user_stats.losses }}</div>
            </div>
            <div class="user-info-item">
                <h3>平局</h3>
                <div class="value">{{ user_stats.draws }}</div>
            </div>
        </div>
    </div>

    <!-- 统计概览 -->
    <div class="stats-overview">
        <div class="stat-card">
            <h3>平均得分</h3>
            <div class="value">{{ user_stats.avg_score|floatformat:1 }}</div>
        </div>
        
        <div class="stat-card">
            <h3>最高得分</h3>
            <div class="value">{{ user_stats.max_score|default:"0" }}</div>
        </div>
        
        <div class="stat-card">
            <h3>代码数量</h3>
            <div class="value">{{ user_codes.count }}</div>
        </div>
        
        <div class="stat-card">
            <h3>活跃代码</h3>
            <div class="value">{{ user_codes|length }}</div>
        </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="content-sections">
        <!-- 最近对战 -->
        <div class="section">
            <h2>🕒 最近对战</h2>
            {% for battle in recent_battles|slice:":10" %}
            <div class="battle-item">
                <div class="battle-info">
                    <div class="battle-opponent">
                        vs {% if battle.player1 == user %}{{ battle.player2.username }}{% else %}{{ battle.player1.username }}{% endif %}
                    </div>
                    <div class="battle-details">
                        {{ battle.created_at|date:"Y-m-d H:i" }} | 
                        {% if battle.player1_code %}{{ battle.player1_code.language }}{% endif %} vs 
                        {% if battle.player2_code %}{{ battle.player2_code.language }}{% endif %}
                    </div>
                </div>
                <div>
                    {% if battle.result == 'draw' %}
                        <span class="battle-result result-draw">平局</span>
                    {% elif battle.result == 'player1_win' and battle.player1 == user %}
                        <span class="battle-result result-win">胜利</span>
                    {% elif battle.result == 'player2_win' and battle.player2 == user %}
                        <span class="battle-result result-win">胜利</span>
                    {% elif battle.result == 'player1_win' or battle.result == 'player2_win' %}
                        <span class="battle-result result-loss">失败</span>
                    {% else %}
                        <span class="battle-result" style="background: #e9ecef; color: #495057;">进行中</span>
                    {% endif %}
                </div>
            </div>
            {% empty %}
            <p style="text-align: center; color: #6c757d; padding: 20px;">暂无对战记录</p>
            {% endfor %}
        </div>

        <!-- 用户代码 -->
        <div class="section">
            <h2>💻 用户代码</h2>
            {% for code in user_codes|slice:":5" %}
            <div class="code-item">
                <div class="code-header">
                    <span class="code-title">{{ code.title }}</span>
                    <span class="code-language">{{ code.language }}</span>
                </div>
                <div class="code-status">
                    {% if code.is_active %}
                        <span class="status-active">✅ 活跃</span>
                    {% else %}
                        <span class="status-inactive">⏸️ 非活跃</span>
                    {% endif %}
                    | {{ code.created_at|date:"Y-m-d" }}
                </div>
            </div>
            {% empty %}
            <p style="text-align: center; color: #6c757d; padding: 20px;">暂无代码</p>
            {% endfor %}
        </div>
    </div>

    <!-- 对手统计 -->
    <div class="full-width-section">
        <h2>⚔️ 对手统计</h2>
        {% if opponents_stats %}
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px;">
            {% for opponent, stats in opponents_stats.items %}
            {% with total=stats.wins|add:stats.losses|add:stats.draws %}
            {% if total > 0 %}
            <div class="opponent-item">
                <div>
                    <div class="opponent-name">{{ opponent.username }}</div>
                    <div class="opponent-record">
                        {{ stats.wins }}胜 {{ stats.losses }}负 {{ stats.draws }}平 ({{ total }}场)
                    </div>
                    <div class="win-rate-bar">
                        <div class="win-rate-fill" style="width: {% widthratio stats.wins total 100 %}%;"></div>
                    </div>
                </div>
                <div style="text-align: right;">
                    <div style="font-weight: bold; color: #6f42c1;">
                        {% widthratio stats.wins total 100 %}%
                    </div>
                    <div style="font-size: 0.8em; color: #6c757d;">
                        胜率
                    </div>
                </div>
            </div>
            {% endif %}
            {% endwith %}
            {% endfor %}
        </div>
        {% else %}
        <p style="text-align: center; color: #6c757d; padding: 20px;">暂无对手数据</p>
        {% endif %}
    </div>

    <!-- 返回按钮 -->
    <div style="text-align: center; margin-top: 30px;">
        <a href="{% url 'admin:battles_dashboard' %}" class="button" style="background: #6f42c1; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; display: inline-block;">
            ← 返回仪表板
        </a>
    </div>
</div>
{% endblock %}